<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div id="test">
    <ul>
      <li v-for="(p,index) in persons" :key="p.id">
        {{index+1}}: {{p.id}} -- {{p.name}} -- {{p.age}}
        --<button @click="deleteP(index)">删除</button>
        --<button @click="reaplceP(index, {id: 9, name: 'Jerry', age: 18})">替换</button>
      </li>
    </ul>
  </div>

  <script src="../js/vue.js"></script>
  <script>
    const vm = new Vue({
      el: "#test",
      data: {
        persons: [
          {id: 3, name: 'Tom', age: 14},
          {id: 5, name: 'Jack', age: 15},
          {id: 7, name: 'Bob', age: 13},
        ]
      },

      methods: {
        deleteP (index) {
          this.persons.splice(index, 1)  // splice不是原生的, 而是vue包装后的
        },

        reaplceP (index, newP) {
          // this.persons[index] = newP  // 界面不会自动更新
          this.persons.splice(index, 1, newP)
        }
      }
    })
    /* 
    Vue如何实现数组数据的响应式?
      不是: 通过defineProperty给元素添加getter/setter
      对数组更新元素的7个方法进行包装(push/unshift/pop/shift/splice/sort/reverse)
        第一步: 调用原生方法对数组进行更新
        第二步: 去更新DOM
    要想更新数组是响应式, 必须调用这7个方法才可以, 不能通过下标来更新
    */

  </script>
</body>
</html>